﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SkinnedModel;

namespace SkinningSample
{
    class Boss : ObjectBase
    {
        private Model model;
        private AnimationPlayer animationPlayer;

        public Model Modle {
            get { return model; }
            set {
                this.model = value;
                SkinningData skinningData = model.Tag as SkinningData;

                if (skinningData == null)
                    throw new InvalidOperationException
                        ("This model does not contain a SkinningData tag.");

                animationPlayer = new AnimationPlayer(skinningData);

                AnimationClip clip = skinningData.AnimationClips["Take 001"];

                animationPlayer.StartClip(clip);
            }
        }

        public override void Update(GameTime gameTime)
        {
            animationPlayer.Update(gameTime.ElapsedGameTime, true, Matrix.Identity);
        }
 
        public override void Draw(GameTime gameTime, Matrix view, Matrix projection)
        {
            Matrix[] bones = animationPlayer.GetSkinTransforms();

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (SkinnedEffect effect in mesh.Effects)
                {
                    effect.SetBoneTransforms(bones);

                    effect.World = Matrix.CreateScale(this.Scale) * Matrix.CreateRotationY(this.Rotation) * Matrix.CreateTranslation(this.Position);
                    effect.View = view;
                    effect.Projection = projection;

                    // デフォルトのスキンメッシュのテクスチャをテスト用テクスチャに張り替え
                    //effect.Parameters["Texture"].SetValue(testTexture);

                    effect.EnableDefaultLighting();

                    effect.SpecularColor = new Vector3(0.25f);
                    effect.SpecularPower = 16;
                }

                mesh.Draw();
            }

        }
    }
}
